/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.danigarcia.android.blacklist;


import org.danigarcia.android.blacklist.database.ConfigManager;
import org.danigarcia.android.blacklist.database.DataManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Base class for all the Activities of the application.
 * Provides basic common functionality as, dialog creation, debug
 * @author Daniel Garcia [contacto {at} danigarcia.org]
  */
public abstract class BaseActivity extends Activity {

	private static final String 	TAG = BaseActivity.class.getName();
	
	protected String orderBy 	= null;
	protected String direction 	= "asc";
	
	protected Cursor cSelect	= null;
	protected ListView listView	= null;
	protected DataManager dm	= null;
	
	protected CursorAdapter cursorAdapter = null;
	
	protected abstract void configureActivity();
	protected abstract CursorAdapter getAdapter(String filter, String orderBy);
	
	/**
	 * Shows the "About" dialog
	 */
	protected void about()
	{
		AboutDialog dialog = new AboutDialog(this);
		dialog.show();
	}
	
	/**
	 * Finishes the current application
	 */
	protected void endApplication()
	{
		finish();
		System.exit(0);
	}

	/**
	 * Manages the ORDER BY clause of a SELECT.
	 * Fist time called, it will make a clause 'orderField asc'. If called a second time
	 * with the same field, it changes to 'orderField desc' and so on.
	 * If the field is changed, it changes again to 'newOrderField asc'
	 * @param order Order field
	 * @return Order field including the order orientation
	 */
	protected String getOrderBy(String order)
	{
		Log.d(TAG, "getOrderBy");
		if(orderBy == null)
		{
			orderBy = order;
			direction = "asc";
		}
		else if(orderBy.equals(order))
			direction = direction.equals("asc") ? "desc" : "asc";

		else
		{
			direction = "asc";
			orderBy = order;
		}
		
		return order + " " + direction;
	}
	
	/**
	 * Loads the default ListView. The database access must be implemented in the
	 * getAdapter method
	 * @param filter Value expected by the database method to filter the results (i.e. "555123932")
	 * @param orderBy order by element (i.e. "phoneNumber desc")
	 */
	protected void loadListView(String filter, String orderBy)
	{
		Log.d(TAG, "loadListView()");
		if((dm != null) && (listView != null))
		{
			Log.d(TAG, "loadListView(): CURSOR NOT NULL");
			cursorAdapter = getAdapter(filter, orderBy);
			listView.setAdapter(cursorAdapter);
		}
		else
			Log.w(TAG, "loadListView(): ELEMENTOS NULL");
		
		/*
		new Handler().post(new Runnable() {
			
			private final Cursor cursor = cSelect;
			
            @Override
            public void run() {
                BlockedPhoneListCursorAdapter adapter = new BlockedPhoneListCursorAdapter(BlockedNumbersActivity.this, cursor);
                Log.d(TAG, "Handler.post().run()");
                lvBlackList.setAdapter(adapter);
                Log.d(TAG, "Handler.post().run(): DONE");
            }
        });
        */
	}
	
	/**
	 * Creates a new AlertDialog with the given parameters.
	 * @param title Title of the Dialog
	 * @param msg Message shown inside the Dialog
	 * @param onOkListener Listener which perform the action after pressing 'OK' button. If not provided, it will close the dialog
	 * @return Brand-new dialog ready to show.
	 */
	protected AlertDialog createConfirmDialog(String title, String msg, DialogInterface.OnClickListener onOkListener)
	{
	    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
	    alertDialogBuilder.setTitle(title);
	    alertDialogBuilder.setMessage(msg);
	 
	    DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
	   	 
	        @Override
	        public void onClick(DialogInterface dialog, int which) {
	        	finish();
	        }

	    };
	    
	    DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() {
	 
	        @Override
	        public void onClick(DialogInterface dialog, int which) {
	            return;
	        }

	    };
	 
	   	alertDialogBuilder.setPositiveButton(R.string.accept, (onOkListener == null) ? okListener : onOkListener);
	    alertDialogBuilder.setNegativeButton(R.string.cancel, cancelListener);
	 
	    return alertDialogBuilder.create();
	}
	
	/**
	 * Shows a toast briefly with the given message
	 * @param message
	 */
	protected void toast(String message)
	{
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	// LOGGING ENCAPSULATED METHODS
	// ----------------------------------------------------------------------
	
	protected void d(String tag, String message)
	{
		if(ConfigManager.DEBUG_LOGGING)
			Log.d(tag, message);
	}
	
	protected void e(String tag, String message)
	{
		e(tag, message, null);
	}
	
	protected void e(String tag, String message, Exception e)
	{
		if(ConfigManager.ERROR_LOGGING)
		{
			if(e == null)
				Log.e(tag, message);
			else
				Log.e(tag, message, e);
		}
	}
	
	protected void w(String tag, String message)
	{
		if(ConfigManager.WARNING_LOGGING)
			Log.w(tag, message);
	}
	
	protected void i(String tag, String message)
	{
		if(ConfigManager.INFO_LOGGING)
			Log.i(tag, message);
	}
	
	protected void v(String tag, String message)
	{
		if(ConfigManager.VERBOSE_LOGGING)
			Log.v(tag, message);
	}
}
